<template>
  <div class="todoapp">
    <TodoHeader :list="list" @addTaskFn="add"></TodoHeader>
    <TodoMain :list="showList" @delTaskFn="del"></TodoMain>
    <TodoFooter
      :list="showList"
      @tggleFn="toggle"
      @clearFn="clear"
    ></TodoFooter>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("toodeList")) || [],
      i: 0,
    };
  },
  methods: {
    add(task) {
      this.list.push({
        id: this.list.length ? this.list[this.list.length - 1].id + 1 : 100,
        name: task,
        isDone: false,
      });
    },
    del(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    toggle(index) {
      this.i = index;
    },
    clear() {
      this.list = this.list.filter((item) => item.isDone == false);
    },
  },
  watch: {
    list: {
      deep: true,
      handler(nweVal) {
        localStorage.setItem("toodeList", JSON.stringify(nweVal));
      },
    },
  },
  computed: {
    showList() {
      if (this.i === 0) {
        return this.list;
      } else if (this.i === 1) {
        return this.list.filter((item) => item.isDone === false);
      } else if (this.i === 2) {
        return this.list.filter((item) => item.isDone === true);
      }
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>

<style lang="scss" scoped></style>
